# apis/charging_station_api.py
from typing import List

from fastapi import APIRouter, HTTPException

from daos.charging_station_dao import ChargingStationDAO
from models.charging_station import ChargingStationDB

from database import get_db_connection

# 创建一个独立的 APIRouter
router = APIRouter(
    prefix="/charging_stations",
    tags=["充电站"]
)

@router.get("/", response_model=List[ChargingStationDB])
async def read_charging_stations():
    try:
        async with get_db_connection() as db_conn:
            dao = ChargingStationDAO(db_conn)
            stations = await dao.get_all()
            return stations
    except Exception as e:
        print(f"查询充电站数据时出错: {e}")
        raise HTTPException(status_code=500, detail="内部服务器错误")

@router.get("/{station_id}", response_model=ChargingStationDB)
async def read_charging_station(station_id: str):
    try:
        async with get_db_connection() as db_conn:
            dao = ChargingStationDAO(db_conn)
            station = await dao.get_by_id(station_id)
            if not station:
                raise HTTPException(status_code=404, detail="充电站未找到")
            return station
    except Exception as e:
        print(f"查询单个充电站时出错: {e}")
        raise HTTPException(status_code=500, detail="内部服务器错误")

# 可以添加 POST, PUT, DELETE 等路由
# @router.post("/", response_model=ChargingStationDB)
# async def create_charging_station(station: ChargingStationCreate, db_conn: asyncpg.Connection = Depends(get_db_connection)):
#     ...
